package com.fishbone.jwt;

import com.fishbone.jwt.config.JwtProperties;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName JwtTokenRedisStorage
 * @Author huyaxi
 * @DATE 2020/4/2 10:16
 */
public class JwtTokenRedisStorage implements JwtTokenStorage {

    private RedisTemplate<String,Object> redisTemplate;

    private JwtProperties jwtProperties;

    public JwtTokenRedisStorage(RedisTemplate<String,Object> redisTemplate,JwtProperties jwtProperties){
        this.redisTemplate = redisTemplate;
        this.jwtProperties = jwtProperties;
    }

    @Override
    public JwtTokenPair put(JwtTokenPair jwtTokenPair, String aud) {
        redisTemplate.boundValueOps(aud).set(jwtTokenPair,jwtProperties.getAccessExpTime(), TimeUnit.MINUTES);
        return jwtTokenPair;
    }

    @Override
    public void expire(String aud) {
        redisTemplate.delete(aud);
    }

    @Override
    public JwtTokenPair get(String key) {
        return (JwtTokenPair) redisTemplate.boundValueOps(key).get();
    }
}
